//
//  AttribRect.cpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/13.
//

#include "STDSAttribRect.hpp"

#include <memory>

using namespace std;

const float g_TexRect[] = {
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f
};

const float g_TexRectFlipV[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f
};

static const float g_TexRectFlipH[] = {
        1.0f, 0.0f,
        0.0f, 0.0f,
        1.0f, 1.0f,
        0.0f, 1.0f
};

const float g_VexRect[] = {
    -1.0f, -1.0f,
    1.0f, -1.0f,
    -1.0f, 1.0f,
    1.0f, 1.0f
};

const float g_VexRectFlipH[] = {
        1.0f, -1.0f,
        -1.0f, -1.0f,
        1.0f, 1.0f,
        -1.0f, 1.0f
};

STDSAttribRect::STDSAttribRect() {
    mCountParticle = 1;
    mCountVertices = 4;
    mUseTex = true;
    mDirtyTex = true;
    std::memcpy(mTargetTexCoord, g_TexRect, sizeof(mTargetTexCoord));
    std::memcpy(mTargetVertices, g_VexRect, sizeof(mTargetVertices));
    _reset();
}

STDSAttribRect::~STDSAttribRect() {

}


void STDSAttribRect::flipTexCoord() {
    std::memcpy(mTargetTexCoord, g_TexRectFlipV, sizeof(mTargetTexCoord));
    mDirtyTex = true;
    _reset();
}

void STDSAttribRect::flipTexCoordH() {
    std::memcpy(mTargetTexCoord, g_TexRectFlipH, sizeof(mTargetTexCoord));
    mDirtyTex = true;
    _reset();
}

void STDSAttribRect::resetTexCoord() {
    std::memcpy(mTargetTexCoord, g_TexRect, sizeof(mTargetTexCoord));
    mDirtyTex = true;
    _reset();
}

void STDSAttribRect::flipHVexCoord() {
    std::memcpy(mTargetVertices, g_VexRectFlipH, sizeof(mTargetVertices));
    mDirtyVex = true;
    _reset();
}

void STDSAttribRect::resetVexCoord() {
    std::memcpy(mTargetVertices, g_VexRect, sizeof(mTargetVertices));

    mDirtyVex = true;
    _reset();
}

void STDSAttribRect::resetVexCoord_Corner(float lbx, float lby, float rbx, float rby, float ltx, float lty, float rtx, float rty) {
    mTargetVertices[0] = lbx;
    mTargetVertices[1] = lby;
    mTargetVertices[2] = rbx;
    mTargetVertices[3] = rby;
    mTargetVertices[4] = ltx;
    mTargetVertices[5] = lty;
    mTargetVertices[6] = rtx;
    mTargetVertices[7] = rty;

    mDirtyVex = true;
    _reset();
}


void STDSAttribRect::_resetDrawFunc(){
    if(mUseIdx)
        mDrawFunc = STDSGLFunctions::draw_ELE_TRI_STRIP;
    else
        mDrawFunc = STDSGLFunctions::draw_ARR_TRI_STRIP;
}

void STDSAttribRect::_resetCount() {
    mCountVertices = mCountParticle * 4;
}

void STDSAttribRect::_resetIdx(){
    SAFE_DELETE_ARRAY(mArrIdx);
    if (mUseIdx) {
        mArrIdx = new uint16_t[mCountVertices];
        for (int i = 0; i < mCountVertices; ++i) {
            mArrIdx[i] = (uint16_t) i;
        }
    }
}

void STDSAttribRect::_resetVex() {
    SAFE_DELETE_ARRAY(mArrVex);
    mArrVex = new float[mCountVertices * 3];
    for (int i = 0; i < mCountVertices; ++i) {
        mArrVex[i * 3] = mTargetVertices[i * 2];
        mArrVex[i * 3 + 1] = mTargetVertices[i * 2 + 1];
        mArrVex[i * 3 + 2] = mDepth;
    }
}

void STDSAttribRect::_resetTex(){
    SAFE_DELETE_ARRAY(mArrTex);
    if(mUseTex){
        mArrTex = new float[mCountVertices * 2];
        for (int i = 0; i < 8; ++i) {
            mArrTex[i] = mTargetTexCoord[i];
        }
    }
}

void STDSAttribRect::_resetClr(){
    SAFE_DELETE_ARRAY(mArrClr);
}
